﻿using System;
using System.Windows.Forms;

namespace SanGuoXiuGai
{
    public partial class frmUpdate : Form
    {
        private KeyboardHook k_hook;
        public frmUpdate()
        {
            InitializeComponent();
        }

        private void frmUpdate_Load(object sender, EventArgs e)
        {
            k_hook = new KeyboardHook();
            k_hook.KeyDownEvent += new KeyEventHandler(hook_KeyDown);//钩住键按下  
            k_hook.Start();//安装键盘钩子
        }

        private void hook_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == (int)Keys.F5)
            {
                if(WindowState == FormWindowState.Normal)
                {
                    this.WindowState = FormWindowState.Minimized;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                    this.Focus();
                }
                
            }
            else if (e.KeyValue == (int)Keys.F6)
            {
                BtnMax_Click(null, null);
                SaveData();
            }
            else if (e.KeyValue == (int)Keys.F7)
            {
                SaveData();
            }
        }

        private void frmUpdate_FormClosed(object sender, FormClosedEventArgs e)
        {
            k_hook.Stop();
        }

        private void BtnRead_Click(object sender, EventArgs e)
        {
            MemoryHelper helper = new MemoryHelper("Tks");
            if (helper.CurrentProcess == null)
            {
                MessageBox.Show("游戏未启动");
                return;
            }
                
            // 等级
            TxtGrade.Text = helper.ReadValue(0x757F0C).ToString();
            // 经验
            TxtExperience.Text = helper.ReadValue(0x757F0E).ToString();
            // 生命
            TxtLife1.Text = helper.ReadValue(0x757F12).ToString();
            TxtLife2.Text = helper.ReadValue(0x757F18).ToString();
            // 内力
            TxtInternalForce1.Text = helper.ReadValue(0x757F14).ToString();
            TxtInternalForce2.Text = helper.ReadValue(0x757F1C).ToString();
            // 攻击
            TxtAttack.Text = helper.ReadValue(0x757F20).ToString();
            // 防御
            TxtDefence.Text = helper.ReadValue(0x757F24).ToString();
            // 命中
            TxtHit.Text = helper.ReadValue(0x757F28).ToString();
            // 闪避
            TxtDodge.Text = helper.ReadValue(0x757F2C).ToString();
            // 速度
            TxtSpeed.Text = helper.ReadValue(0x757F30).ToString();
            // 幸运
            TxtLucky.Text = helper.ReadValue(0x757F34).ToString();
            // 移动
            TxtMove.Text = helper.ReadValue(0x757F38).ToString();
            // 悟性
            TxtSavvy.Text = helper.ReadValue(0x757F3C).ToString();
            // 技能点
            TxtSkill.Text = helper.ReadValue(0x757F46).ToString();
            // 金钱
            TxtMoney.Text = helper.ReadValue(0x756882).ToString();
            // 灵魂
            TxtSoul.Text = helper.ReadValue(0x757F04).ToString();
            // 兽元
            TxtMonster.Text = helper.ReadValue(0x757F06).ToString();
            // 勇气
            TxtWisdom.Text = helper.ReadValue(0x756884).ToString();
            // 仁德
            TxtCourage.Text = helper.ReadValue(0x756886).ToString();
            // 智慧
            TxtKindness.Text = helper.ReadValue(0x756888).ToString();
            // 医术
            TxtMedicine.Text = helper.ReadValue(0x75688A).ToString();
        }

        private void BtnMax_Click(object sender, EventArgs e)
        {
            // 等级
            TxtGrade.Text = "60";
            // 经验
            TxtExperience.Text = "30000";
            // 生命
            TxtLife1.Text = "9999";
            TxtLife2.Text = "9999";
            // 内力
            TxtInternalForce1.Text = "9999";
            TxtInternalForce2.Text = "9999";
            // 攻击
            TxtAttack.Text = "9999";
            // 防御
            TxtDefence.Text = "9999";
            // 命中
            TxtHit.Text = "999";
            // 闪避
            TxtDodge.Text = "999";
            // 速度
            TxtSpeed.Text = "999";
            // 幸运
            TxtLucky.Text = "999";
            // 移动
            TxtMove.Text = "99";
            // 悟性
            TxtSavvy.Text = "999";
            // 技能点
            TxtSkill.Text = "30000";
            // 金钱
            TxtMoney.Text = "30000";
            // 灵魂
            TxtSoul.Text = "9999";
            // 兽元
            TxtMonster.Text = "9999";
            // 勇气
            TxtWisdom.Text = "9999";
            // 仁德
            TxtCourage.Text = "9999";
            // 智慧
            TxtKindness.Text = "9999";
            // 医术
            TxtMedicine.Text = "9999";
        }

        private short StrToShort(string value)
        {
            short result = 0;
            short.TryParse(value, out result);
            return result;
        }

        private void SaveData()
        {
            MemoryHelper helper = new MemoryHelper("Tks");
            if (helper.CurrentProcess == null)
            {
                MessageBox.Show("游戏未启动");
                return;
            }
                
            // 等级
            helper.WriteValue(0x757F0C, BitConverter.GetBytes(StrToShort(TxtGrade.Text.ToString())));
            // 经验
            helper.WriteValue(0x757F0E, BitConverter.GetBytes(StrToShort(TxtExperience.Text.ToString())));
            // 生命
            helper.WriteValue(0x757F12, BitConverter.GetBytes(StrToShort(TxtLife1.Text.ToString())));
            helper.WriteValue(0x757F16, BitConverter.GetBytes(StrToShort(TxtLife1.Text.ToString())));
            helper.WriteValue(0x757F18, BitConverter.GetBytes(StrToShort(TxtLife2.Text.ToString())));
            // 内力
            helper.WriteValue(0x757F14, BitConverter.GetBytes(StrToShort(TxtInternalForce1.Text.ToString())));
            helper.WriteValue(0x757F1A, BitConverter.GetBytes(StrToShort(TxtInternalForce1.Text.ToString())));
            helper.WriteValue(0x757F1C, BitConverter.GetBytes(StrToShort(TxtInternalForce2.Text.ToString())));
            // 攻击
            helper.WriteValue(0x757F20, BitConverter.GetBytes(StrToShort(TxtAttack.Text.ToString())));
            // 防御
            helper.WriteValue(0x757F24, BitConverter.GetBytes(StrToShort(TxtDefence.Text.ToString())));
            // 命中
            helper.WriteValue(0x757F28, BitConverter.GetBytes(StrToShort(TxtHit.Text.ToString())));
            // 闪避
            helper.WriteValue(0x757F2C, BitConverter.GetBytes(StrToShort(TxtDodge.Text.ToString())));
            // 速度
            helper.WriteValue(0x757F30, BitConverter.GetBytes(StrToShort(TxtSpeed.Text.ToString())));
            // 幸运
            helper.WriteValue(0x757F34, BitConverter.GetBytes(StrToShort(TxtLucky.Text.ToString())));
            // 移动
            helper.WriteValue(0x757F38, BitConverter.GetBytes(StrToShort(TxtMove.Text.ToString())));
            // 悟性
            helper.WriteValue(0x757F3C, BitConverter.GetBytes(StrToShort(TxtSavvy.Text.ToString())));
            // 技能点
            helper.WriteValue(0x757F46, BitConverter.GetBytes(StrToShort(TxtSkill.Text.ToString())));
            // 金钱
            helper.WriteValue(0x756882, BitConverter.GetBytes(StrToShort(TxtMoney.Text.ToString())));
            // 灵魂
            helper.WriteValue(0x757F04, BitConverter.GetBytes(StrToShort(TxtSoul.Text.ToString())));
            // 兽元
            helper.WriteValue(0x757F06, BitConverter.GetBytes(StrToShort(TxtMonster.Text.ToString())));
            // 勇气
            helper.WriteValue(0x756884, BitConverter.GetBytes(StrToShort(TxtWisdom.Text.ToString())));
            // 仁德
            helper.WriteValue(0x756886, BitConverter.GetBytes(StrToShort(TxtCourage.Text.ToString())));
            // 智慧
            helper.WriteValue(0x756888, BitConverter.GetBytes(StrToShort(TxtKindness.Text.ToString())));
            // 医术
            helper.WriteValue(0x75688A, BitConverter.GetBytes(StrToShort(TxtMedicine.Text.ToString())));
        }

    }
}
